package com.example.day2.busi.my;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.content.Intent;
import android.content.pm.Capability;
import android.os.Bundle;
import android.view.View;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.blankj.utilcode.util.SPUtils;
import com.bumptech.glide.Glide;
import com.example.day2.R;
import com.example.day2.busi.login.LoginActivity;
import com.example.day2.databinding.ActivitySetingBinding;
import com.example.day2.entity.IconResult;
import com.example.day2.entity.UpDateAddress;
import com.example.day2.entity.UpDateBrith;
import com.example.day2.entity.UpDateSexResult;
import com.example.day2.entity.VideoResult;
import com.example.day2.mvvm.base.BaseActivity;
import com.wildma.pictureselector.PictureBean;
import com.wildma.pictureselector.PictureSelector;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class SettingActivity extends BaseActivity<ActivitySetingBinding,MyViewModel> {
   private String s;
   private  String format1;
   private String s1;

    @Override
    public void initView() {
        //退出登录
        binding.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SPUtils.getInstance().clear();
                startActivity(new Intent(SettingActivity.this, LoginActivity.class));
            }
        });
        //返回
        binding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               finish();
            }
        });
        //性别
        binding.tv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<String> list = new ArrayList<>();
                list.add("男");
                list.add("女");
                list.add("其他");

                OptionsPickerView<String> pickerView = new OptionsPickerBuilder(SettingActivity.this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        s = list.get(options1);
                        viewModel.getSex(s);

                    }
                }).build();
                pickerView.setPicker(list);
                pickerView.show();

            }
        });
        //生日
        binding.tv3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new TimePickerBuilder(SettingActivity.this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                         format1 = format.format(date);
                         viewModel.getBrith(format1);
                    }
                }).build().show();
            }
        });
        //更改头像
        binding.tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PictureSelector.create(SettingActivity.this,PictureSelector.SELECT_REQUEST_CODE).selectPicture();

            }
        });
        //地址
        binding.tv4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<String> list = new ArrayList<>();
                list.add("山西省");
                list.add("河北省");
                list.add("河南省");
                list.add("山东省");
                List<List<String>> data = new ArrayList<>();

                List<String> xi = new ArrayList<>();
                xi.add("太原市");
                xi.add("大同市");
                xi.add("阳泉市");
                xi.add("晋中市");

                List<String> nan = new ArrayList<>();
                nan.add("濮阳市");
                nan.add("开封市");
                nan.add("洛阳市");
                nan.add("郑州市");

                List<String> bei = new ArrayList<>();
                bei.add("石家庄市");
                bei.add("保定市");
                bei.add("唐山市");
                bei.add("廊坊市");

                List<String> dong = new ArrayList<>();
                dong.add("菏泽市");
                dong.add("济南市");
                dong.add("德州市");
                dong.add("临沂市");

                data.add(xi);
                data.add(bei);
                data.add(nan);
                data.add(dong);




                OptionsPickerView<String> build = new OptionsPickerBuilder(SettingActivity.this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                         s1 = list.get(options1) + data.get(options1).get(options2);
                        viewModel.getAddress(s1);
                    }
                }).build();
                build.setPicker(list,data);
                build.show();
            }
        });

    }

    @Override
    public void initData() {
        String sex = SPUtils.getInstance().getString("sex");
        String address = SPUtils.getInstance().getString("address");
        String icon = SPUtils.getInstance().getString("icon");
        String birth = SPUtils.getInstance().getString("birth");
        if (icon!=null){
            Glide.with(this).load(icon).circleCrop().into(binding.iv);
        }

        if (sex!=null){
            binding.tv2.setText("性别："+sex);
        }
        if (address != null){
            binding.tv4.setText("地址："+address);
        }
        if (birth != null){
            binding.tv3.setText("生日："+birth);
        }
        viewModel.successSex.observe(this, new Observer<UpDateSexResult>() {
            @Override
            public void onChanged(UpDateSexResult upDateSexResult) {
                if (upDateSexResult.getCode() == 200){
                    binding.tv2.setText("性别："+s);
                    SPUtils.getInstance().put("sex",s);
                }
            }
        });
        viewModel.successIcon.observe(this, new Observer<IconResult>() {
            @Override
            public void onChanged(IconResult iconResult) {
                if (iconResult.getCode() == 200){
                    Glide.with(SettingActivity.this).load(iconResult.getData()).circleCrop().into(binding.iv);
                    SPUtils.getInstance().put("icon",iconResult.getData());
                }
            }
        });
        viewModel.successBrith.observe(this, new Observer<UpDateBrith>() {
            @Override
            public void onChanged(UpDateBrith upDateBrith) {
                if (upDateBrith.getCode() == 200){
                    binding.tv3.setText("生日："+format1);
                    SPUtils.getInstance().put("birth",format1);
                }
            }
        });
        viewModel.successAddress.observe(this, new Observer<UpDateAddress>() {
            @Override
            public void onChanged(UpDateAddress upDateAddress) {
                if (upDateAddress.getCode() == 200){
                    binding.tv4.setText("地址："+s1);
                    SPUtils.getInstance().put("address",s1);
                }
            }
        });
    }

    //拍照


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE && resultCode == RESULT_OK){
            Bundle extras = data.getExtras();
            PictureBean pictureBean = (PictureBean) extras.get(PictureSelector.PICTURE_RESULT);
            String path = pictureBean.getPath();
           viewModel.getIcon(path);
        }
    }

    @Override
    public Class<MyViewModel> getVIewModel() {
        return MyViewModel.class;
    }

    @Override
    public int bindLayoutId() {
        return R.layout.activity_seting;
    }
}